package cn.aitrox.ry.service.auth.controller;

import cn.aitrox.ry.bean.RespResult;
import cn.aitrox.ry.enumtype.RespCodeEnum;
import cn.aitrox.ry.service.auth.api.AuthGroupApi;
import cn.aitrox.ry.service.auth.dto.group.in.AuthGroupCreateInDto;
import cn.aitrox.ry.service.auth.dto.group.in.AuthGroupUpdateInDto;
import cn.aitrox.ry.service.auth.dto.group.out.AuthGroupDetailDto;
import cn.aitrox.ry.service.auth.service.AuthGroupService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class AuthGroupController implements AuthGroupApi {

    @Autowired
    private AuthGroupService authGroupService;


    @PostMapping(value = "/auth/group")
    @Override
    public RespResult<Integer> create(@Validated @RequestBody AuthGroupCreateInDto input) {
        // 参数校验与处理
        if (StringUtils.isBlank(input.getName())) {
            return RespResult.secByError(RespCodeEnum.PARAM_ERROR.getCode(), "名称不能为空");
        }
        List<Integer> roleIds = null == input.getRoleIds() ? new ArrayList<>() : input.getRoleIds().stream().distinct().collect(Collectors.toList());
        List<Integer> userIds = null == input.getUserIds() ? new ArrayList<>() : input.getUserIds().stream().distinct().collect(Collectors.toList());
        input.setName(input.getName().trim());
        input.setRoleIds(roleIds);
        input.setUserIds(userIds);
        return authGroupService.create(input);
    }

    @PutMapping(value = "/auth/group")
    @Override
    public RespResult update(@Validated @RequestBody AuthGroupUpdateInDto input) {
        String name = null;
        if (null != input.getName()) {
            if (StringUtils.isBlank(input.getName())) {
                return RespResult.secByError(RespCodeEnum.PARAM_ERROR.getCode(), "名称不能为空");
            }
            name = input.getName().trim();
        }
        List<Integer> roleIds = null == input.getRoleIds() ? new ArrayList<>() : input.getRoleIds().stream().distinct().collect(Collectors.toList());
        List<Integer> userIds = null == input.getUserIds() ? new ArrayList<>() : input.getUserIds().stream().distinct().collect(Collectors.toList());
        input.setName(name);
        input.setRoleIds(roleIds);
        input.setUserIds(userIds);

        return authGroupService.update(input);
    }

    @GetMapping(value = "/auth/group{id}")
    @Override
    public RespResult<AuthGroupDetailDto> read(@PathVariable("id") Integer id) {
        return authGroupService.read(id);
    }

    @DeleteMapping(value = "/auth/group/{id}")
    @Override
    public RespResult delete(@PathVariable("id") Integer id) {
        return authGroupService.delete(id);
    }

    @GetMapping(value = "/auth/group/all")
    @Override
    public List<AuthGroupDetailDto> all() {
        return authGroupService.all();
    }
}
